//
//  PalistModel.swift
//
//  Created by eme on 2017/1/3
//  Copyright (c) . All rights reserved.
//

import Foundation
import SwiftyJSON
import ModelProtocol

class PalistModel: ModelProtocol ,Copyable{
    
    // MARK: Declaration for string constants to be used to decode and also serialize.
    internal let kPalistPavalueKey: String = "pavalues"
    internal let kPalistPanameKey: String = "paname"
    
    
    // MARK: 属性
    var pavalues: [PavalueModel]
    var paname: String
    /// 用于显示已选中的 属性
    var showSubStr:String{
        get {
            var relArr:[String] = []
            for item in pavalues{
                if item.hasSelected {
                    relArr.append(item.pavalue)
                }
            }
            if relArr.count > 0 {
                return  relArr.joined(separator: ",")
            }else{
                return "不限"
            }
        }
    }
    
    func copy() -> Copyable {
        let pa = PalistModel(json: "")!
        pa.pavalues = self.pavalues.map{ $0.copy() as! PavalueModel}
        pa.paname = self.paname
        return pa
    }
    
    /// 表示是否展开
    var isSpreaded:Bool = false
    // MARK: 实现MikerSwiftJSONAble 协议， 解析json数据
    required  init?(json: JSON) {
        pavalues = []
        if let items = json[kPalistPavalueKey].array {
            for item in items {
                if let tempValue = item.string {
                    pavalues.append(PavalueModel(pavalue: tempValue))
                }
            }
        } else {
            pavalues = []
        }
        paname = json[kPalistPanameKey].stringValue
        
    }
    
    
    /**
     Generates description of the object in the form of a NSDictionary.
     - returns: A Key value pair containing all valid values in the object.
     */
    internal func dictionaryRepresentation() -> [String : AnyObject ] {
        
        var dictionary: [String : AnyObject ] = [ : ]
        
        dictionary.updateValue(pavalues as AnyObject, forKey: kPalistPavalueKey)
        
        dictionary.updateValue(paname as AnyObject, forKey: kPalistPanameKey)
        
        
        return dictionary
    }
    
    
}
